package com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.constant.DangerousManagementConstant;
import com.xidian.zhiancloud.dao.DangerousChemicalInventoryChangeRecordMapper;
import com.xidian.zhiancloud.model.DangerCategory;
import com.xidian.zhiancloud.model.DangerousChemicalInfo;
import com.xidian.zhiancloud.model.DangerousChemicalInventoryChangeRecord;
import com.xidian.zhiancloud.model.DangerousChemicalStorage;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.chemicalsManagement.DangerousChemicalIcrQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.CodeService;
import com.xidian.zhiancloud.service.systemSetting.SysPositionService;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalIcrVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Egoist
 * @Date: 2020/11/10 17:00
 */

@Service
public class DangerousChemicalInventoryChangeRecordService extends ServiceImpl<DangerousChemicalInventoryChangeRecordMapper, DangerousChemicalInventoryChangeRecord> implements IService<DangerousChemicalInventoryChangeRecord>{

    @Autowired
    private DangerousChemicalInventoryChangeRecordMapper dangerousChemicalInventoryChangeRecordMapper;
    @Autowired
    private CodeService codeService;
    @Autowired
    private UserService userService;
    @Autowired
    private DangerCategoryService dangerCategoryService;
    @Autowired
    private DangerousChemicalInfoService dangerousChemicalInfoService;
    @Autowired
    private DangerousChemicalStorageService dangerousChemicalStorageService;
    @Autowired
    private SysPositionService sysPositionService;

    public IPage<DangerousChemicalIcrVO> getListByQO(DangerousChemicalIcrQO dangerousChemicalIcrQO) {
        Page<DangerousChemicalIcrVO> page = new Page<>(dangerousChemicalIcrQO.getCurrent(), dangerousChemicalIcrQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("storage_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<DangerousChemicalIcrVO> ipage = dangerousChemicalInventoryChangeRecordMapper.getListByQO(page, dangerousChemicalIcrQO);
        List<DangerousChemicalIcrVO> records = ipage.getRecords();
        List<DangerousChemicalIcrVO> ans = new ArrayList<>();
        records.forEach(t -> {
            t.setStrQuantity(t.getStorageQuantity().toString() + DangerousManagementConstant.QuantityUnit.get(t.getUnit()));
            ans.add(t);
        });
        ipage.setRecords(ans);
        return ipage;
    }

    public Boolean add(DangerousChemicalInventoryChangeRecord dangerousChemicalInventoryChangeRecord) {

        String code = codeService.getChemicalStorageCode(dangerousChemicalInventoryChangeRecord.getCompanyId());
        dangerousChemicalInventoryChangeRecord.setStorageCode(code);

        return save(dangerousChemicalInventoryChangeRecord);
    }

    public Boolean export(HttpServletResponse response, Long[] ids) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("危化品入库台账", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<DangerousChemicalInventoryChangeRecord> list = listByIds(Arrays.asList(ids));
        List<DangerousChemicalIcrVO> ans = new LinkedList<>();
        list.forEach(t -> {
            DangerousChemicalIcrVO vo = new DangerousChemicalIcrVO();
            BeanUtils.copyProperties(t, vo);
            SysUser user = userService.getById(vo.getStoragePersonId());
            DangerousChemicalStorage dangerousChemicalStorage = dangerousChemicalStorageService.getById(vo.getStorageId());
            DangerousChemicalInfo chemicalInfo = dangerousChemicalInfoService.getById(dangerousChemicalStorage.getChemicalId());
            SysPosition pos = sysPositionService.getById(dangerousChemicalStorage.getPositionId());
            DangerCategory dangerCategory = null;
            String unit = "";
            if (chemicalInfo != null){
                dangerCategory = dangerCategoryService.getById(chemicalInfo.getDangerousTypeId());
                unit = DangerousManagementConstant.QuantityUnit.get(chemicalInfo.getUnit());
            }

            vo.setStoragePersonName(user == null ? "该用户已删除" : user.getName());
            vo.setDangerousType(dangerCategory == null ? "该类型已删除" : dangerCategory.getName());
            vo.setChemicalName(chemicalInfo == null ? "该危化品信息已删除" : chemicalInfo.getName());
            vo.setAlias(chemicalInfo == null ? "该危化品信息已删除" : chemicalInfo.getAlias());
            vo.setCas(chemicalInfo == null ? "该危化品信息已删除" : chemicalInfo.getCasNum());
            vo.setCode(chemicalInfo == null ? "该危化品信息已删除" : chemicalInfo.getCode());
            vo.setPositionName(pos == null ? "该存放位置已删除" : pos.getName());
            vo.setStrQuantity(vo.getStorageQuantity().toString() + unit);
            ans.add(vo);

        });

        EasyExcel.write(response.getOutputStream(), DangerousChemicalIcrVO.class).sheet("模板").doWrite(ans);
        return true;
    }

}
